package com.adgroup.smartnote;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.jar.Attributes.Name;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.androidquery.AQuery;
import com.google.android.gcm.GCMRegistrar;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import database.Database;

import ImageLoaderUrl.ImageLoader;
import ImageLoaderUrl.Utils;
import JsonModel.User;
import JsonModel.UserLogo;
import Net.JsonParse;
import Utilities.Configuration;
import Utilities.Utility;
import adapter.AdapterUserLogo;
import adapter.AdapterUserLogo.Adapter;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import animationmenu.CollapseAnimation;
import animationmenu.ExpandAnimation;

public class RegisterActivity extends SherlockFragmentActivity{

	private DisplayMetrics metrics;
	private LinearLayout menuPanel;
	private int panelWidth;
	private LinearLayout slidingPanel;
	private boolean isExpanded;

	FrameLayout.LayoutParams menuPanelParameters;
	FrameLayout.LayoutParams slidingPanelParameters;
	LinearLayout.LayoutParams headerPanelParameters;
	LinearLayout.LayoutParams listViewParameters;
	public static ListView lvLogo;
	RelativeLayout.LayoutParams lvcategoriesParameters;
	
	//views
	Button btnAvatar;
	EditText etgmail;
	EditText etnickname;
	EditText etPass;
	EditText etRePass;
	CheckBox chkMale;
	CheckBox chkFemale;
	TextView tvBirthday;
	StringBuilder sBirthday;
	LinearLayout linearRoot;
	ImageButton btnBirthday;
	Button btnRegister;
	Spinner spinner;
	ProgressBar progressWaiting;
	ImageView ivAvatar;
	
	private int mYear;
	private int mMonth;
	private int mDay;
	private int logoid = 0;
	private String gcmId = "";
	private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
	ProgressDialog progress;
	Handler handler = new Handler();
	LinearLayout root;
	GoogleCloudMessaging gcm;
	
	
	//adapter
	AdapterUserLogo.Adapter adapLogo;
	private ActionBar mActionBar;
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
		case android.R.id.title:
			finish();
			break;		
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	@SuppressLint("CutPasteId")
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.register);
		
		//action bar
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(false);
		mActionBar.setDisplayUseLogoEnabled(true);
		mActionBar.setHomeButtonEnabled(true);
		mActionBar.setTitle(getResources().getString(R.string.buttonlogin));
		
		metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		
		//set up
		panelWidth = (int) ((metrics.widthPixels) * 0.7);
		menuPanel = (LinearLayout) findViewById(R.id.menuPanelRegister);
		menuPanelParameters = (FrameLayout.LayoutParams) menuPanel
				.getLayoutParams();
		menuPanelParameters.width = panelWidth;
		menuPanel.setLayoutParams(menuPanelParameters);
		slidingPanel = (LinearLayout) findViewById(R.id.slidemainregister);
		slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel
				.getLayoutParams();
		slidingPanelParameters.width = metrics.widthPixels;
		slidingPanel.setLayoutParams(slidingPanelParameters);

		lvLogo = (ListView) findViewById(R.id.listViewuserlogo);
		lvcategoriesParameters = (RelativeLayout.LayoutParams) lvLogo
				.getLayoutParams();
		lvcategoriesParameters.width = metrics.widthPixels;
		lvLogo.setLayoutParams(lvcategoriesParameters);
		
		//mapping
		btnAvatar = (Button) findViewById(R.id.ButtonAvatar);
		tvBirthday = (TextView) findViewById(R.id.txtbirthdayfill);
		chkFemale = (CheckBox) findViewById(R.id.checkfemale);
		chkMale = (CheckBox) findViewById(R.id.checkmale);
		etgmail = (EditText) findViewById(R.id.editTextGmail);
		etnickname = (EditText) findViewById(R.id.editTextNickName);
		etPass = (EditText) findViewById(R.id.editTextPassword);
		etRePass = (EditText) findViewById(R.id.editTextRePassWord);
		linearRoot = (LinearLayout) findViewById(R.id.slidemainregister);
		btnBirthday = (ImageButton) findViewById(R.id.btnbirthday);
		btnRegister = (Button) findViewById(R.id.btnregister);
		spinner = (Spinner) findViewById(R.id.spinneruserlogo);
		progressWaiting =(ProgressBar) findViewById(R.id.progressBarwaitinguserlogo);
		ivAvatar = (ImageView) findViewById(R.id.imageViewAvatar);
		root = (LinearLayout) findViewById(R.id.rootregister);
		
		//set male
		chkMale.setChecked(true);
		//add item for spinner
		String[] gender = getResources().getStringArray(R.array.gender);
		ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,gender);
		adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adp1);
		//load logo
		
		
		//register gcm id
		new AsynGetGCMID().execute();
		
		//GCMRegistrar.checkDevice(this);
		/*GCMRegistrar.checkManifest(this);
		// Get GCM registration id
		gcmId = GCMRegistrar.getRegistrationId(this);
		GCMRegistrar.setRegisterOnServerLifespan(getApplicationContext(),
				604800000);	 
		if (gcmId.equals("")) {
			GCMRegistrar.register(this, Configuration.SENDERID);
		} else {
			long limittime = GCMRegistrar.getRegisterOnServerLifespan(this);
		
			if (GCMRegistrar.isRegisteredOnServer(this)) {
				// Skip Register;
			} else {
				GCMRegistrar.register(this, Configuration.SENDERID);
			}
		}
		//broadcast to receive gcm id
		BroadcastReceiver receiverRefresh = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				String yourData = intent.getStringExtra("regid");
				if (yourData != null) {
					gcmId = yourData;
					GCMRegistrar.setRegisteredOnServer(context, true);
				}

			}
		};*/
		
		//
		ShowKeyboard(etgmail);
		mYear = 1991;
		mMonth = 11;
		mDay = 1;
		updateBirthday();
		btnBirthday.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				DatePickerDialog DPD = new DatePickerDialog(RegisterActivity.this,dateSetListener, mYear, mMonth, mDay);
				DPD.show();	
				updateBirthday();
			}
		});
		chkMale.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if(arg1){
					chkFemale.setChecked(false);
				}
				else{
					chkFemale.setChecked(true);
				}
			}
		});
		chkFemale.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if(arg1){
					chkMale.setChecked(false);
				}
				else{
					chkMale.setChecked(true);
				}
			}
		});
		linearRoot.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(etgmail.getWindowToken(), 0);
				imm.hideSoftInputFromWindow(etnickname.getWindowToken(), 0);
				imm.hideSoftInputFromWindow(etPass.getWindowToken(), 0);
				imm.hideSoftInputFromWindow(etRePass.getWindowToken(), 0);
			
			}
		});
		btnAvatar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if (!isExpanded) {
					isExpanded = true;

					// Expand
					new ExpandAnimation(slidingPanel, panelWidth,
							Animation.RELATIVE_TO_SELF, 0.0f,
							Animation.RELATIVE_TO_SELF, 0.7f, 0, 0.0f, 0, 0.0f);
				} else {
					isExpanded = false;

					// Collapse
					new CollapseAnimation(slidingPanel, panelWidth,
							TranslateAnimation.RELATIVE_TO_SELF, 0.7f,
							TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
							0, 0.0f);

				}
			}
		});
		//load logo
		new AsynLoadLogo().execute(); 
		lvLogo.setVisibility(View.GONE);
		progressWaiting.setVisibility(View.VISIBLE);
		lvLogo.setOnTouchListener(new OnTouchLogo());
		lvLogo.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				logoid = ((UserLogo) (adapLogo.getItem(arg2))).getLogoid();
				final String url = ((UserLogo) (adapLogo.getItem(arg2))).getLink();
				Log.d("log",url);
				AQuery aq = new AQuery(arg1);
				Bitmap placeholder = aq.getCachedImage(R.drawable.imageload);
				aq.id(ivAvatar).image(url, true,
							true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK, 1.0f);
				
				isExpanded = false;
				new CollapseAnimation(slidingPanel, panelWidth,
						TranslateAnimation.RELATIVE_TO_SELF, 0.7f,
						TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0,
						0.0f);
			}
		});
		
		//spinner change
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if(adapLogo!=null){
					if(arg2==0){
						adapLogo.setIsMale(1);					
					}
					else if(arg2==1){
						adapLogo.setIsMale(0);
					}
					adapLogo.notifyDataSetChanged();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		//register
		btnRegister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String  gmail = etgmail.getText().toString();
				String nickname = etnickname.getText().toString();
				String pass = (etPass.getText().toString());
				String retype = (etRePass.getText().toString());
				boolean isEmail = Utility.isEmailValid(gmail);
				StringBuilder birthday = new StringBuilder().append(mYear).append("-").append(mMonth + 1).append("-").append(mDay);
				String isGender;
				Date now = new Date();
				Integer checkYear = mYear - Integer.parseInt(formatter.format(now));

				if(chkFemale.isChecked()){
					isGender = "0";
				}
				else{
					isGender = "1";
				}
				
				//check infor
				if(gmail.equalsIgnoreCase("")){
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.errortitleregister))
					.setMessage(getResources().getString(R.string.errornullgmailregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									ShowKeyboard(etgmail);
								}
					}).show();
				}
				else if(!isEmail){
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.errortitleregister))
					.setMessage(getResources().getString(R.string.errorninvalidgmailregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									ShowKeyboard(etgmail);
								}
					}).show();
				}
				else if(nickname.equalsIgnoreCase("")){
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.errortitleregister))
					.setMessage(getResources().getString(R.string.errornullnicknameregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									ShowKeyboard(etnickname);
								}
					}).show();
				}
				else if(nickname.length()<6){
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.errortitleregister))
					.setMessage(getResources().getString(R.string.error6nicknameregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									ShowKeyboard(etnickname);
								}
					}).show();
				}
				else if(pass.equalsIgnoreCase("")){
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.errortitleregister))
					.setMessage(getResources().getString(R.string.errornullpassregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									ShowKeyboard(etPass);
								}
					}).show();
				}
				else if(pass.length()<6){
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.errortitleregister))
					.setMessage(getResources().getString(R.string.error6passregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									ShowKeyboard(etPass);
								}
					}).show();
				}
				else if(retype.equalsIgnoreCase("")){
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.errortitleregister))
					.setMessage(getResources().getString(R.string.errornullrepassregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									ShowKeyboard(etRePass);
								}
					}).show();
				}
				else if(!retype.equalsIgnoreCase(pass)){
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.errortitleregister))
					.setMessage(getResources().getString(R.string.errormatchespassregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									ShowKeyboard(etPass);
								}
					}).show();
				}
				else if(checkYear>-5){
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.errortitleregister))
					.setMessage(getResources().getString(R.string.errorbirthdayregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									DatePickerDialog DPD = new DatePickerDialog(RegisterActivity.this,dateSetListener, mYear, mMonth, mDay);
									DPD.show();										
								}
					}).show();
				}
				else {
					if(!Utility.isConnect(RegisterActivity.this)){
						new AlertDialog.Builder(RegisterActivity.this)
						.setTitle(getResources().getString(R.string.titleerrornetwork))
						.setMessage(getResources().getString(R.string.messageerrornetwork))
						.setNegativeButton(getResources().getString(R.string.setting), new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								
							}
						})
						.setPositiveButton(getResources().getString(R.string.cancel),
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
									}
						}).show();
					}
					else{
						pass = Utility.md5(pass);
						retype = Utility.md5(retype);
						String[] params = new String[] {gmail,nickname,pass,isGender+"",mYear+"-"+mMonth+"-"+mDay,0+"","0",gcmId,logoid+""};
						new AsynRegister().execute(params);
					}
				}
				
			}
		});
	}
	//show keyboard
	public void ShowKeyboard(EditText et)
	{
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
	}
	//
	protected void updateBirthday() {
		tvBirthday.setText(new StringBuilder()
				.append(mDay).append("-").append(mMonth + 1).append("-")
				.append(mYear));
	}
	private OnDateSetListener dateSetListener = new OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
			mYear = arg1;
			mMonth = arg2;
			mDay = arg3;
			updateBirthday();
		}
	};
	
	//implement on touch logo
	class OnTouchLogo implements OnTouchListener
	{
		static final String logTag = "OnTouchLogo";
		static final int MIN_DISTANCE = 200;
		private float downX, downY, upX, upY;

		public void onRightToLeftSwipe() {
			Log.d("item", "RightToLeftSwipe!");
			if (isExpanded) {
				isExpanded = false;

				// Collapse
				new CollapseAnimation(slidingPanel, panelWidth,
						TranslateAnimation.RELATIVE_TO_SELF, 0.7f,
						TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0,
						0.0f);
			}
		}

		public void onLeftToRightSwipe() {
			Log.d("item", "LeftToRightSwipe!");
			if (!isExpanded) {
				isExpanded = true;

				// Expand
				new ExpandAnimation(slidingPanel, panelWidth,
						Animation.RELATIVE_TO_SELF, 0.0f,
						Animation.RELATIVE_TO_SELF, 0.7f, 0, 0.0f, 0, 0.0f);
			}
		}

		public void onTopToBottomSwipe() {
			Log.d("logTag", "onTopToBottomSwipe!");
			// activity.doSomething();
		}

		public void onBottomToTopSwipe() {
			Log.d("logTag", "onBottomToTopSwipe!");
			// activity.doSomething();
		}

		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN: {
				downX = event.getX();
				downY = event.getY();
				return true;
			}
			case MotionEvent.ACTION_UP: {
				upX = event.getX();
				upY = event.getY();

				float deltaX = downX - upX;
				float deltaY = downY - upY;
				Log.d("item", deltaX + ":" + downX + "-" + upX);
				// swipe horizontal?
				if (Math.abs(deltaX) > MIN_DISTANCE) {
					// left or right
					Log.d("item", deltaX + "âa");
					if (deltaX < 0) {
						this.onLeftToRightSwipe();
						return true;
					}
					if (deltaX > 0) {
						this.onRightToLeftSwipe();
						return true;
					}
				} else {
					Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
							+ " long, need at least " + MIN_DISTANCE);
					return false; // We don't consume the event
				}
				return true;
			}
			}
			return false;
		}
		
	}
	//asyntask regisster
	class AsynRegister extends AsyncTask<String, Void, String>
	{
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progress = ProgressDialog.show(RegisterActivity.this, "",
					getResources().getString(R.string.registering), true);
		}
		@Override
		protected String doInBackground(String... params) {
			return new JsonParse().registerAccount(params,RegisterActivity.this);
		}
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progress.dismiss();
			JSONObject json;
			try {
				json = new JSONObject(result);
				String res = json.getString(Configuration.result);
				if(res.equalsIgnoreCase("-1")){
					Utility.ShowAlert(RegisterActivity.this, getResources().getString(R.string.errortitleregister), getResources().getString(R.string.severbusytregister));
				}
				else if(res.equalsIgnoreCase("0")){
					Utility.ShowAlert(RegisterActivity.this, getResources().getString(R.string.errortitleregister), getResources().getString(R.string.errornexistgmailregister));
				}
				else{
					JSONObject rjson = json.getJSONArray(Configuration.result).getJSONObject(0);
					String gmai = rjson.getString(User.keyGmail);
					String nickname = rjson.getString(User.keyNickName);
					String birthday = rjson.getString(User.keyBirthday);
					String type = rjson.getString(User.keyType);
					String gender = rjson.getString(User.keyGender);
					String link = rjson.getString(User.keyLink);
					final String userid = rjson.getString(User.keyUserId);
					User u = new User(userid, nickname, gender, birthday, link, type, gmai);
					new Database(RegisterActivity.this).setUser(u);
					new AlertDialog.Builder(RegisterActivity.this)
					.setTitle(getResources().getString(R.string.successtitleregister))
					.setMessage(getResources().getString(R.string.successmessageregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Intent returnIntent = new Intent();
									returnIntent.putExtra("result", etnickname.getText().toString());									
									setResult(RESULT_OK, returnIntent);
									finish();									
								}
					}).show();
				}
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
	//get gcm id
	class AsynGetGCMID extends AsyncTask<Void, Void, Void>
	{

		@Override
		protected Void doInBackground(Void... params) {
			if(gcm==null){
				//GCMRegistrar.checkManifest(RegisterActivity.this);
				gcm = GoogleCloudMessaging.getInstance(RegisterActivity.this);
				try {
					gcmId = gcm.register(Configuration.SENDERID);
					Log.d("log","gcm:"+gcmId);
				} catch (IOException e) {
					gcmId = "";
					Log.e("error",e.getMessage());
				}
			}
			return null;
		}
		
	}
	//asyntask load avatar
	class AsynLoadLogo extends AsyncTask<Void, Void, String>
	{
		ArrayList<UserLogo> logos = new ArrayList<UserLogo>();
		@Override
		protected String doInBackground(Void... params) {			
			return new JsonParse().getUserLogo(RegisterActivity.this);			
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			try {
				JSONObject json = new JSONObject(result);
				JSONArray results = json.getJSONArray("result");
				for(int i=0;i<results.length();i++){
					JSONObject obj = results.getJSONObject(i);
					String gender = obj.getString(UserLogo.keyGender);
					String link = obj.getString(UserLogo.keyLink);
					int logoid = Integer.parseInt(obj.getString(UserLogo.keyLogoId));
					logos.add(new UserLogo(gender,link,logoid));
				}
				adapLogo = new AdapterUserLogo().new Adapter(RegisterActivity.this, logos, 1);
				lvLogo.setAdapter(adapLogo);
				progressWaiting.setVisibility(View.GONE);
				lvLogo.setVisibility(View.VISIBLE);
				 Log.d("log","finished");
			} catch (JSONException e) {
				Log.d("error",e.getMessage());
				e.printStackTrace();
			}
			
		}
	}
}
